package com.svs.framework.util;

import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Random;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class RandomUtil {
	
	private static String[] enPos = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
	
	private static String BIG_EN = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
	private static String SMALL_EN = "abcdefghijklmnopqrstuvwxyz" ;
	private static String DIGIT = "0123456789" ;
	

	private RandomUtil() {
	}

	static Logger logger = LoggerFactory.getLogger(RandomUtil.class);



	public synchronized String getId(String prefix) {
		SimpleDateFormat sdf = new SimpleDateFormat("yy{0}dd{1}mmssSSS");
		Calendar calendar = Calendar.getInstance();
		String month = enPos[calendar.get(Calendar.MONTH)];
		String hour = enPos[calendar.get(Calendar.HOUR_OF_DAY)];
		String result = prefix.concat(sdf.format(calendar.getTime()));
		result = MessageFormat.format(result, month, hour);
		return result;
	}


	public static String randStringDigit(int length) {
		return randString(DIGIT, length);
	}

	public static String randString(int length) {
		String source = BIG_EN.concat(SMALL_EN).concat(DIGIT) ; //"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
		return randString(source, length);
	}

	public static String randString(String source, int length) {
		char[] src = source.toCharArray();
		char[] buf = new char[length];
		Random ran = new Random();

		for (int i = 0; i < length; i++) {
			int rnd = ran.nextInt(src.length);
			buf[i] = src[rnd];
		}
		return new String(buf);
	}

}
